#include "pump.h"

Pump::Pump(int dimension, int status, QGraphicsItem *parent):QGraphicsItem(parent), m_dimension(dimension)
{
    createBrush();
    m_status = status;
}

void Pump::createBrush(){
    m_gradient_green = QLinearGradient(0, 0, 0, 100);
    m_gradient_green.setColorAt(0.0, QColor(0,255,127));
    m_gradient_green.setColorAt(1.0, QColor(0,205,102));

    m_gradient_red = QLinearGradient(0, 0, 0, 100);
    m_gradient_red.setColorAt(0.0, QColor(255,0,0));
    m_gradient_red.setColorAt(1.0, QColor(192,0,0));
}

void Pump::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){

    // disegna il cerchio (ellisse)
    painter->setPen(Qt::NoPen);

    if(!m_status){
        painter->setBrush(m_gradient_green);
    }
    else{
        painter->setBrush(m_gradient_red);
    }

    QPainterPath path;
    path.setFillRule(Qt::OddEvenFill);
    path.addEllipse(boundingRect());
    painter->drawPath(path);

    // disegna il triangolo interno
    QPainter *p8(painter);
    if(!m_status){
        p8->setBrush(QColor(34,139,34));
    }
    else{
        p8->setBrush(QColor(178,34,34));
    }
    qreal x = m_dimension/4;
    qreal val = sqrt(pow(2*x,2)-pow(x,2));
    qreal y1 = 2 * x - val;
    qreal y2 = m_dimension - y1;

    QPointF point0(m_dimension, m_dimension/2);
    QPointF point1(x,y1);
    QPointF point2(x,y2);

    static const QPointF points[4] = {point0, point1, point2};
    p8->drawPolygon(points,3);
}

QRectF Pump::boundingRect() const{
    return QRectF(0,0,m_dimension, m_dimension);
}

void Pump::changeStatus(){
    // XOR: se lo stato era 1, diventa 0 e viceversa
    m_status = m_status^1;
    emit statusChanged();
}

void Pump::statusChanged(){}
